import ET

eFacePlain      = 0
eFaceBold       = 1
eFaceItalic     = 2
eFaceUnderline  = 4

_gFaceMap = {
	"Medium" : eFacePlain,
	"Bold" : eFaceBold,
	"Oblique" : eFaceItalic,
	"BoldOblique": eFaceBold|eFaceItalic
}
def CharStyle(face=eFacePlain,size=-1,rgb=(0,0,0),font=-1):
	if size==-1:
		# take the size of 'Code' as default
		x,size,x=GetNamedSpec("Code")
	return ET.CharStyle(font, face, size, rgb)

def NamedCharStyle(name):
	try:
		return ET.CharStyle(name)
	except:
		return NamedPrefStyle(name)
def GetNamedSpec(name):
	"""Get a (face, size, rgb) with NAME from preferences."""
	from string import atoi,split,splitfields
	try:
		font=splitfields(ET.GetPreference("Sniff."+name+".Font"),'-')
		color=split(ET.GetPreference("Sniff."+name+".Color"))
		face=eFacePlain
		if _gFaceMap.has_key(font[1]):
			face=_gFaceMap[font[1]]
		sz=atoi(font[2])
		r=atoi(color[2])
		g=atoi(color[3])
		b=atoi(color[4])
		return face,sz,(r,g,b)
	except:
		return eFaceBold,-1,(0,0,0)
 
def NamedPrefStyle(name):
	"""Get a CharStyle with NAME from preferences."""
	face,size,rgb,font=GetNamedSpec(name)
	return CharStyle(face,size,rgb)
